package cst.neu.yuejian_31.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.UserInfoBean;
import cst.neu.yuejian_31.bean.UserUpdateBean;
import cst.neu.yuejian_31.iface.UserInfoListener;
import cst.neu.yuejian_31.iface.UserUpdateListener;
import cst.neu.yuejian_31.model.UserInfoModel;
import cst.neu.yuejian_31.model.UserUpdateModel;

public class UserUpdateActivity extends BaseActivity implements View.OnClickListener,UserInfoListener,UserUpdateListener {

    private EditText userName,userTel,userPassword;
    private ImageView userPhoto,btn_return;
    private Button btn_update;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_update);
        initViews();
        initEvents();
        initData();
    }

    private void initViews() {
        userName= (EditText) findViewById(R.id.editText_username);
        userPassword= (EditText) findViewById(R.id.editText_password);
        userTel= (EditText) findViewById(R.id.editText_tel);
        userPhoto= (ImageView) findViewById(R.id.imageView_userphoto_up);
        btn_update= (Button) findViewById(R.id.button_update);
        btn_return= (ImageView) findViewById(R.id.imageView_returnuser);
    }
    private void initEvents(){
        btn_update.setOnClickListener(this);
        btn_return.setOnClickListener(this);

    }
    private  void initData(){
        String userid=getUserId();
        UserInfoModel model=new UserInfoModel();
        model.getUserInfo(userid,this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_update:
                UserUpdateModel updateModel=new UserUpdateModel();
                updateModel.userUpdate(getUserId(),
                        userName.getText().toString(),
                        userPassword.getText().toString(),
                        userTel.getText().toString(),this);
                break;
            case R.id.imageView_returnuser:
                finish();
                break;
        }

    }

    @Override
    public void onResponse(UserInfoBean user) {
        userName.setText(user.getUsername());
        userTel.setText(user.getUserphone());
        userPassword.setText(user.getPassword());
        Picasso.with(UserUpdateActivity.this).load(Constant.USER_URL+user.getUser_pic()).into(userPhoto);
    }

    @Override
    public void onResponse(UserUpdateBean userUpdateBean) {
        Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        finish();

    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
    }
}
